export default {
  components: {

  },
  data() {
    return {
      recommend: true, //是否显示i推荐
      // carlist: [],//好车推荐
      // list: [],
      show: true, //历史搜索
      searchKey: '',
      hotList: [],
      // history: [],
      loadStatus: 'loading',
      // carIndex: 1,//当前页
      // listShow: false,//车源列表
      searchList: [],
      resultList: [],
      filterList: [],
      isBlock: true,
    }
  },
  created() {
    this.getCarhot()
    this.getSearchList()

  },

  methods: {

    onSearchList(name) {
      console.log(name)
      this.searchKey = name
      uni.navigateTo({
        url: '/pages_commodity/doMaintenance/doMaintenance?keyword=' + name
      })
    },
    async getSearchList() {
      try {
        const res = await this.$api.jsonData.MaintenanceTypeAndGoods()
        // maintenance_Type_Name
        this.searchList = res.data.data
      } catch (e) {
        //TODO handle the exception
      }
    },
    //热门搜索
    hotSearch(list) {
      this.list = []
      this.carIndex = 1
      this.searchKey = list.words
      uni.navigateTo({
        url: '/pages_commodity/doMaintenance/doMaintenance?keyword=' + list.words
      })

    },
    //历史搜索
    onhistory(item) {
      this.list = []
      this.carIndex = 1
      this.searchKey = item
      this.listShow = true
      this.show = false
      this.recommend = false

    },
    // 加载前、中、后
    moreStatus(status) {
      this.loadStatus = status
    },
    //点击搜索
    textSearch() {
      this.list = []
      this.carIndex = 1
      if (this.searchKey === '') {
        this.$u.toast('请输入关键字')
        return false
      } else {
        uni.navigateTo({
          url: '/pages_commodity/doMaintenance/doMaintenance?keyword=' + this.searchKey
        })
      }
    },
    //搜索change
    change(e) {
      this.searchKey = e
      if (e === '') {
        this.resultList = []
        this.show = true
        this.filterList = []
        return false
      }
      //过滤关键字
      if (this.searchList.length > 0) {
        this.resultList = this.searchList.filter(v => {
          return JSON.stringify(v.maintenance_Type_Name).indexOf(this.searchKey.toUpperCase()) !== -1 //JSON.stringify()是必须的
        })
      }
      this.show = false
    },
    brightenKeyword(val, keyword) {
      // 方法2：用正则表达式
      const Reg = new RegExp(keyword, 'i')
      if (val) {
        const res = val.replace(Reg, `<span style="color: #FBD220;">${keyword}</span>`)
        // console.log(res);
        return res
      }
    },
    //连接方法
    join(str, key) {
      var reg = new RegExp((`(${key})`), 'gm')
      var replace = '<span style="color:#FBD220;">$1</span>'
      return str.replace(reg, replace)
    },
    //清楚缓存
    del() {
      var that = this
      uni.setStorage({
        key: 'searchMaintenance',
        data: that.history
      })
      this.history = []
    },
    //搜索
    onSearch(val) {
      this.list = []
      this.carIndex = 1
      if (val === '') {
        this.$u.toast('请输入关键字')
        return false
      }
      uni.navigateTo({
        url: '/pages_commodity/doMaintenance/doMaintenance?keyword=' + val
      })
      this.searchKey = val

    },
    //热门搜索
    async getCarhot() {
      try {
        const res = await this.$api.maintenanceIndex.getHotWordsResponses()

        this.hotList = res.data
      } catch (e) {
        //TODO handle the exception
      }
    },
  }
}
